#!/bin/bash
set -eux

### --start_docs
## Setup repositories for the job
## ==============================

## Prepare Your Environment
## ------------------------

## * Execute commands before repositories setup
## ::

{{ repo_cmd_before|default('') }}
{{ dep_repo_cmd_before|default('') }}

# Make temp dir for files
sudo rm -rf /tmp/repo_role_tmp && mkdir -p /tmp/repo_role_tmp

## * Prepare repositories
## ::

# Repos can be defined in repos, add_repos (used mostly in
# featuresets and in the job itself

# creating the following repos
{% for repo in repos + add_repos + job.add_repos|default([]) %}
{% if repo.reponame is defined %}
# repo = {{ repo.reponame }}
{% else %}
# repo.reponame is not defined
{% endif %}
{% endfor %}

{% for repo in repos + add_repos + job.add_repos|default([]) %}
{% if repo.releases is not defined or release in repo.releases %}

{% if repo.type == 'file' %}

## * Download and use file from {{ repo.down_url }}
## ::

curl -Lvo /tmp/repo_role_tmp/{{ repo.filename }} {{ repo.down_url }}
{% if repo.priority is defined and repo.priority %}

## * Change its priority to {{ repo.priority }}
## ::

if ! grep -q "priority" /tmp/repo_role_tmp/{{ repo.filename }}; then
    sed -i "s/\(baseurl=.*\)/\1\npriority={{ repo.priority }}/g" /tmp/repo_role_tmp/{{ repo.filename }}
else
    sed -i "s/priority=.*/priority={{ repo.priority }}/g" /tmp/repo_role_tmp/{{ repo.filename }}
fi
{% endif %}
{% if repo.reponame is defined %}
sed -i "s/\[.*\]/[{{ repo.reponame }}]/" /tmp/repo_role_tmp/{{ repo.filename }}
{% endif %}
{% if repo.includepkgs is defined %}
echo -e '\nincludepkgs={{ repo.includepkgs|join(",") }}' >> /tmp/repo_role_tmp/{{ repo.filename }}
{% endif %}
sudo cp -f /tmp/repo_role_tmp/{{ repo.filename }} /etc/yum.repos.d/
{% endif %}

{% if repo.type == 'generic' %}

## * Construct repo file from arguments
## ::

{% if repo.hash_url is defined %}
HASH=$(curl {{ repo.hash_url }} 2>/dev/null | grep baseurl | grep -Eo '[^/]*/[^/]*/[^/]*_.*' ||:)
{% endif %}
cat<<EOF >/tmp/repo_role_tmp/{{ repo.filename }}
[{{ repo.reponame }}]
name={{ repo.reponame }}
{% if repo.baseurl is defined %}baseurl={{ repo.baseurl }}
{% endif %}
{% if repo.mirrorlist is defined %}mirrorlist={{ repo.mirrorlist }}
{% endif %}
gpgcheck={{ repo.gpgcheck|default('0') }}
enabled={{ repo.enabled|default('1') }}
{% if repo.priority is defined %}priority={{ repo.priority }}
{% endif %}
{% if repo.module_hotfixes is defined %}
module_hotfixes={{ repo.module_hotfixes|default('0') }}
{% endif %}
{% if repo.exclude is defined %}exclude={{ repo.exclude|join(",") }}
{% endif %}
{% if repo.includepkgs is defined %}includepkgs={{ repo.includepkgs|join(",") }}
{% endif %}
{% if repo.down_url is defined %}# trunk_hash=${HASH:-}
{% endif %}
{% if repo.sslverify is defined %}
sslverify={{ repo.sslverify|default('1') }}
{% endif %}
EOF
sudo cp -f /tmp/repo_role_tmp/{{ repo.filename }} /etc/yum.repos.d/
{% endif %}

{% if repo.type == 'package' %}

## * Install repo from package {{ repo.pkg_name|default(repo.pkg_url) }}
## ::

{% if repo.custom_cmd is defined %}
{{ repo.custom_cmd }} {{ repo.pkg_name|default(repo.pkg_url) }}
{% else %}
sudo  {{ ansible_pkg_mgr }} install -y {{ repo.pkg_name|default(repo.pkg_url) }}
{% endif %}

{% endif %}

{% endif %}
{% endfor %}

{% if use_rhsm|bool  %}

## * Install repo from rhsm subscription manager
## ::

subscription-manager register --username {{ rhsm_username | quote }} --password {{ rhsm_password | quote }}
subscription-manager attach --pool={{ pool_id }}
subscription-manager repos --disable=*

{% for repo in rhsm_repos %}

subscription-manager repos --enable={{ repo.name }}

{% endfor %}
{% endif %}


## * Execute commands after repositories setup
## ::

{{ repo_cmd_after|default('') }}
{{ dep_repo_cmd_after|default('') }}

### --stop_docs
